<template>
	<view v-show="loading">
		<view class="cu-form-group margin-top">
			<view class="title">输入邮箱</view>
			<input placeholder="输入邮箱" v-model="phone" v-if="user.email=='' || user.email==null"></input>
			<input placeholder="输入邮箱" v-model="phone" :disabled="true" v-else></input>
		</view>
		<view class="cu-form-group solid-bottom">
			<view class="title">验证码</view>
			<input placeholder="输入获取到的验证码" name="input" v-model='code'></input>
			<button class='cu-btn bg-green shadow' :disabled="disab" @tap="getYzm">{{yzmStatus}}</button>
		</view>
		<view class="padding">
			<button class="cu-btn bg-blue margin-tb-sm lg" style="width: 100%;" @tap="geteditpass">确认提交</button>
		</view>
	</view>
</template>

<script>
	var yzmTime=59,yzmEnable=true;
	export default {
		data() {
			return {
				yzmStatus:"获取验证码",
				disab:false,
				code:null,
				phone:null,
				loading:false,
				user:[],
				type:"newemail"
			}
		},
		onLoad() {
			uni.showLoading({
				
			})
			this.getUsets()
		},
		methods: {
			getUsets(){
				var that=this;
				uni.request({
					url: that.app.apiHost+"/mindex",
					method:'POST',
					header:{'Authorization':'bearer '+that.app.getAuthCode()},
					success:(res)=>{
						console.log(res.data);
						if(res.data.code == -1){
							that.app.showMsg('请登陆后再操作!','../login/login');
						}
						if(res.data.code==0){
							uni.setStorageSync("users",JSON.stringify(res.data.data))
              console.log(788944);
							that.user=res.data.data
							if(that.user.email!="" && that.user.email!=null){
								that.phone=that.user.email
								that.type="editemail"
							}
							uni.hideLoading()
							that.loading=true
						}
					},
					fail(res) {
						console.log(res);
					}
				})
			},
			downTimer:function(){
				var that=this;
				var it=setInterval(function(){
					yzmEnable=false;
					that.yzmStatus="倒计时"+yzmTime+"秒";
					yzmTime--;
					if(yzmTime==0){
						yzmTime=59;
						yzmEnable=true;
						that.disab=false;
						that.yzmStatus="获取验证码";
						clearInterval(it);
					}
				},1000);
			},
			geteditpass(){
				if(this.phone==null){
					this.app.showMsg("请输入手机号");
					return;
				}
				if(this.code==null){
					this.app.showMsg("请输入验证码");
					return;
				}
				uni.showLoading({
					mask:true
				})
				try{
					var that=this;
					var surl="/verifyCode";
					if(this.type=="newemail")surl="/setEmail"
					uni.request({
						url:that.app.apiHost + surl,
						method:"POST",
						header:{'Authorization':'bearer '+that.app.getAuthCode()},
						data:{code:that.code,email:that.phone,type:that.type},
						success:function(res){
							uni.hideLoading()
							if(res.data.code == -1){
								that.app.showMsg('请登陆后再操作!','../login/login');
							}
							if(res.data.code==0){
								
								if(that.type=="newemail"){
									that.app.showMsg('设置新邮箱成功!','../my/my');
									setTimeout(() => {
										uni.switchTab({
											url: '/pages/my/my'
										})
									}, 1000)
								}else{
									
									that.app.showMsg('验证成功，去设置新邮箱!','../my/editemail');
								}
							}else{
								that.app.showMsg(res.data.msg);
							}
						},fail: (e) => {
						}
					})
				}catch(e){
					uni.hideLoading()
					this.app.showMsg("系统发生错误")
				}
				
			},
			getYzm:function(){
					if(!yzmEnable) return false;
					yzmEnable=false
					var that=this;
					uni.request({
						url:that.app.apiHost+"/sendEmailMsg",
						method:"POST",
						header:{'Authorization':'bearer '+that.app.getAuthCode()},
						data:{email:that.phone,type:that.type},
						success:function(res){
							yzmEnable=true
							if(res.data.code==-1){
								that.app.showMsg('请登陆后再操作!','../login/login');
							}
							that.app.showMsg(res.data.msg);
							if(res.data.code==0){
								that.disab=true;
								that.downTimer();
							}
						}
					})
			}
		}
	}
</script>

<style>

</style>
